package com.photoeditor.demo.model.bean.face;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Base64;

import com.common.base.ComponentContext;
import com.common.base.utils.FileUtil;
import com.photoeditor.demo.model.face.FaceConstant;
import com.photoeditor.demo.util.Utils;

import java.io.File;

/**
 * desc：
 * Created by chenli on 2018/11/7
 */
public class AgeResultBean {

    public static final int OLDIFY_TYPE_NONE = 0;
    public static final int OLDIFY_TYPE_50 = 50;
    public static final int OLDIFY_TYPE_70 = 70;
    public static final int OLDIFY_TYPE_90 = 90;

    private static final String IMG_TMP_FLODER = FileUtil.getExternalCacheDir(ComponentContext.getContext(), ".age", false);


    private String code;
    private boolean success;
    private String data;
    private String message;

    private int age;

    /**
     * 原始照片的路径
     */
    private String originalPath;

    /**
     * 小孩图片保存在存储卡里的路径（保存后，需要将pic字段置空，避免Intent中传输时，数据量过大，导致Binder爆内存）
     */
    public String picFilePath;


    /**
     * 将图片二进制数据保存到存储卡
     * 注意：异步操作，需要放线程
     * @return 返回图片保存的路径，保存失败会返回null
     */
    public String savePic() {
        if (TextUtils.isEmpty(data)) {
            return null;
        }
        byte[] data = Base64.decode(this.data, Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        String savePath  = getEffectSavePath(getAge());
        boolean ret = Utils.saveBitmap(bitmap, savePath);
        if (ret) {
            picFilePath = savePath;
            this.data = "";
        }
        return picFilePath;
    }

    private String getEffectSavePath(int age) {
        switch (age) {
            case OLDIFY_TYPE_NONE:
                return originalPath;
            case OLDIFY_TYPE_50:
                return FaceConstant.PATH_PHOTO + File.separator + "effect_50.jpg";
            case OLDIFY_TYPE_70:
                return FaceConstant.PATH_PHOTO + File.separator + "effect_70.jpg";
            case OLDIFY_TYPE_90:
                return FaceConstant.PATH_PHOTO + File.separator + "effect_90.jpg";
            default:
                return null;
        }
    }



    public String getPicFilePath() { return picFilePath; }

    public String getOriginalPath() { return originalPath; }

    public void setOriginalPath(String path) { this.originalPath = path; }

    public int getAge() { return age; }

    public void setAge(int age) { this.age = age; }

    public void setPicFilePath(String picFilePath) {
        this.picFilePath = picFilePath;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


}
